layui.use(['form', 'layer'], function () {
    $ = layui.jquery;
    form = layui.form;
});

//批量删除提交
function delAll() {
    obj = document.getElementsByName("checkbox");   // 获取所有的复选框对象
    check_val = [];
    for (k in obj) {
        if (obj[k].checked) check_val.push(obj[k].value);
    }
    layer.confirm('确认要删除吗？' + check_val, function (index) {
        if (check_val.length > 0) {
            $.ajax({
                type: "DELETE_S",
                data: {"data_s": check_val.join(',')},
                dataType: "json",
                success: function (data) {
                    if (data.status === 0) {
                        layer.msg('删除成功!', {icon: 1});
                        window.location.reload();
                    } else {
                        layer.msg('删除失败!', {icon: 2});
                    }
                }, error: function (data) {
                    layer.msg('操作失败!', {icon: 2, time: 2000});
                }
            });
        } else {
            layer.msg('提示:请选择删除项!', {icon: 2});
        }
    });
}

/*用户-停用*/
function member_stop(obj, id, status) {
    layer.confirm('确认要停用吗？', function (index) {
        //发异步把用户状态进行更改
        $.ajax({
            type: "POST",
            dataType: "json",
            data: {"uid": id, "status": status},
            success: function (data) {
                if (data.status === 0) {
                    $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="member_start(this,id,)" href="javascript:;" title="启用"><i class="layui-icon">&#xe62f;</i></a>');
                    $(obj).parents("tr").find(".td-status").html('<span class="layui-btn layui-btn-disabled layui-btn-mini">已停用</span>');
                    $(obj).remove();
                    layer.msg('已停用!', {icon: 5, time: 1000});
                    window.location.reload();
                } else {
                    layer.msg('操作失败!' + data.message, {icon: 2, time: 2000});
                }
            }, error: function (data) {
                layer.msg('操作失败!', {icon: 2, time: 2000});
            }
        });
    });
}

/*用户-启用*/
function member_start(obj, id, status) {
    layer.confirm('确认要启用吗？', function (index) {
        $.ajax({
            type: "POST",
            dataType: "json",
            data: {"uid": id, "status": status},
            success: function (data) {
                if (data.status === 0) {
                    $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="member_stop(this,id)" href="javascript:;" title="停用"><i class="layui-icon">&#xe601;</i></a>');
                    $(obj).parents("tr").find(".td-status").html('<span class="layui-btn layui-btn-normal layui-btn-mini">已启用</span>');
                    $(obj).remove();
                    layer.msg('已启用!', {icon: 6, time: 1000});
                    window.location.reload();
                } else {
                    layer.msg('操作失败!' + data.message, {icon: 2, time: 2000});
                }
            }, error: function (data) {
                layer.msg('操作失败!', {icon: 2, time: 2000});
            }
        });
    });
}

/*form_提交*/
function submit_post() {
    var data = $("form").serializeArray();
    $.ajax({
        type: 'POST',
        data: data,
        dataType: "json",
        success: function (data) {
            if (data.status === 0) {
                layer.msg('操作成功', {icon: 1, time: 2000});
                var index = parent.layer.getFrameIndex(window.name);
                window.parent.location.reload();//刷新父页面
                parent.layer.close(index);
            } else {
                layer.msg('操作失败: ' + data.message, {icon: 2, time: 3000});
            }
        }, error: function (data) {
            layer.msg('操作失败!', {icon: 2, time: 2000});
        }
    });
    return false;
}

/*form_提交*/
function content_submit() {
    editor.sync();
    editor.html($('#content').val());
    var data = $("form").serializeArray();
    $.ajax({
        type: 'POST',
        data: data,
        dataType: "json",
        success: function (data) {
            if (data.status === 0) {
                layer.msg('操作成功', {icon: 1, time: 2000});
                var index = parent.layer.getFrameIndex(window.name);
                window.parent.location.reload();//刷新父页面
                parent.layer.close(index);
            } else {
                layer.msg('操作失败: ' + data.message, {icon: 2, time: 3000});
            }
        }, error: function (data) {
            layer.msg('操作失败!', {icon: 2, time: 2000});
        }
    });
    return false;
}

/*用户-添加*/
function member_add(title, url, w, h) {
    x_admin_show(title, url, w, h);
}

// 用户-编辑
function member_edit(title, url, id, w, h) {
    x_admin_show(title, url, w, h);
}

// 水平弹出层
function level_add(title, url, id, w, h) {
    x_admin_show(title, url, w, h);
}

/*密码-修改*/
function member_password(title, url, id, w, h) {
    x_admin_show(title, url, w, h);
}

/*删除*/
function member_del(obj, id) {
    layer.confirm('确认要删除吗？', function (index) {
        $.ajax({
            type: "DELETE",
            data: {"uid": id},
            dataType: "json",
            success: function (data) {
                if (data.status === 0) {
                    $(obj).parents("tr").remove();
                    layer.msg('删除成功!', {icon: 1, time: 1000});
                } else {
                    layer.msg('删除失败: ' + data.message, {icon: 2, time: 2000});
                }
            }, error: function (data) {
                layer.msg('操作失败!', {icon: 2, time: 2000});
            }
        });
    });
}


//批量恢复提交
function recoverAll() {
    layer.confirm('确认要批量恢复吗？', function (index) {
        //捉到所有被选中的，发异步进行恢复
        layer.msg('恢复成功', {icon: 1});
    });
}

/*用户-恢复*/
function member_recover(obj, id) {
    layer.confirm('确认要恢复吗？', function (index) {
        $.ajax({
            type: "UPDATE",
            data: {"uid": id},
            dataType: "json",
            success: function (data) {
                if (data.status === 0) {
                    $(obj).parents("tr").remove();
                    layer.msg('恢复成功!', {icon: 1, time: 1000});
                } else {
                    layer.msg('恢复失败: ' + data.message, {icon: 2, time: 2000});
                }
            }, error: function (data) {
                layer.msg('恢复失败!', {icon: 2, time: 2000});
            }
        });
    });
}

/*用户-彻底删除*/
function member_unset(obj, id) {
    layer.confirm('彻底删除无法恢复，确认要删除数据吗？', function (index) {
        $.ajax({
            type: "DELETE",
            data: {"uid": id},
            dataType: "json",
            success: function (data) {
                if (data.status === 0) {
                    $(obj).parents("tr").remove();
                    layer.msg('删除成功!', {icon: 1, time: 1000});
                } else {
                    layer.msg('删除失败: ' + data.message, {icon: 2, time: 2000});
                }
            }, error: function (data) {
                layer.msg('删除失败!', {icon: 2, time: 2000});
            }
        });
    });
}


//批量彻底删除
function delete_unset() {
    obj = document.getElementsByName("checkbox");   // 获取所有的复选框对象
    check_val = [];
    for (k in obj) {
        if (obj[k].checked) check_val.push(obj[k].value);
    }
    layer.confirm('彻底删除无法恢复，确认要删除数据吗？', function (index) {
        if (check_val.length > 0) {
            $.ajax({
                type: "DELETE_S",
                data: {"data_s": check_val.join(',')},
                dataType: "json",
                success: function (data) {
                    if (data.status === 0) {
                        layer.msg('删除成功!', {icon: 1});
                        window.location.reload();
                    } else {
                        layer.msg('删除失败!', {icon: 2, time: 2000});
                    }
                }, error: function (data) {
                    layer.msg('删除失败!', {icon: 2, time: 2000});
                }
            });
        } else {
            layer.msg('提示:请选择删除项!', {icon: 2});
        }

    });
}

// 弹出上传文件窗口
function open_file() {
    document.getElementById('upload').click();
}
function open_qrCode() {
    document.getElementById('upload_qrCode').click();
}

//logo上传
function upload_file(url_up) {
    var formData = new FormData();
    formData.append('file', $('#upload')[0].files[0]);  //添加图片信息的参数
    $.ajax({
        url: url_up,
        type: 'POST',
        cache: false, //上传文件不需要缓存
        data: formData,
        processData: false, // 告诉jQuery不要去处理发送的数据
        contentType: false, // 告诉jQuery不要去设置Content-Type请求头
        success: function (data) {
            if (data.status === 0) {
                $('#web_logo').val(data.img_url);
            } else {
                alert('提示: ' + data.message)
            }
        },
        error: function (data) {
            alert('提示: 上传失败!')
        }
    })
}

//网站二维码上传
function upload_QrCode(url_up) {
    var formData = new FormData();
    formData.append('file', $('#upload_qrCode')[0].files[0]);  //添加图片信息的参数
    $.ajax({
        url: url_up,
        type: 'POST',
        cache: false, //上传文件不需要缓存
        data: formData,
        processData: false, // 告诉jQuery不要去处理发送的数据
        contentType: false, // 告诉jQuery不要去设置Content-Type请求头
        success: function (data) {
            if (data.status === 0) {
                $('#web_QrCode').val(data.img_url);
            } else {
                alert('提示: ' + data.message)
            }
        },
        error: function (data) {
            alert('提示: 上传失败!')
        }
    })
}

//文档缩略图上传
function document_upload_file(url_up) {
    var formData = new FormData();
    formData.append('file', $('#upload')[0].files[0]);  //添加图片信息的参数
    $.ajax({
        url: url_up,
        type: 'POST',
        cache: false, //上传文件不需要缓存
        data: formData,
        processData: false, // 告诉jQuery不要去处理发送的数据
        contentType: false, // 告诉jQuery不要去设置Content-Type请求头
        success: function (data) {
            if (data.status === 0) {
                $('#thumb_img').val(data.img_url);
            } else {
                alert('提示: ' + data.message)
            }
        },
        error: function (data) {
            alert('提示: 上传失败!')
        }
    })
}

// API
function api_set() {
    var data = $("form").serializeArray();
    $.ajax({
        type: 'POST',
        data: data,
        dataType: "json",
        success: function (data) {
            if (data.status === 0) {
                layer.msg('操作成功', {icon: 1, time: 2000});
                window.location.reload();
            } else {
                layer.msg('操作失败: ' + data.message, {icon: 2, time: 3000});
            }
        }, error: function (data) {
            layer.msg('操作失败!', {icon: 2, time: 2000});
        }
    });
    return false;
}